package org.zt.java.dal.util;

import java.util.Map;

import org.aopalliance.intercept.MethodInvocation;
import org.zt.java.dal.config.DalConfig;
import org.zt.java.dal.config.vo.MethodRouterConfig;


public class DalConfigUtil {


	public static MethodRouterConfig getMethodRouteConfig(DalConfig dalConfig,
			MethodInvocation invocation) {
		String className = invocation.getThis().getClass().getName();
		String methodName = invocation.getMethod().getName();
		String param = getMethodParam(invocation);

		return getMethodRouteConfig(dalConfig, className, methodName, param);
	}

	public static MethodRouterConfig getMethodRouteConfig(DalConfig dalConfig,
			String className, String methodName, String param) {
		Map<String, MethodRouterConfig> methodRouterConfigMap = dalConfig
				.getMethodRouteConfigMap();

		{
			// 精确判断class_method_param 是否存在
			MethodRouterConfig methodRouterConfig = methodRouterConfigMap
					.get(getMethodRouteIndexKey(
							className,
							methodName,
							param));
			if (methodRouterConfig != null) {
				return methodRouterConfig;
			}
		}

		{
			// 判断class_method_[*] 是否存在
			MethodRouterConfig methodRouterConfig = methodRouterConfigMap
					.get(getMethodRouteIndexKey(
							className,
							methodName,
							MethodRouterConfig.DEFAULT_PARAM));
			if (methodRouterConfig != null) {
				return methodRouterConfig;
			}
		}
		
		{
			// 判断方法模糊匹配，class_find*_[*] or class_*_[*]
			Map<String, MethodRouterConfig> fuzzyMethodRouterConfigMap = dalConfig.getFuzzyMethodRouteConfigMap();
			for (String key : fuzzyMethodRouterConfigMap.keySet()) {
				if (getFuzzyMethodRouteIndexKey(className, methodName).startsWith(key)) {
					return fuzzyMethodRouterConfigMap.get(key);
				}
			}
		}

		return null;
	}

	@SuppressWarnings("rawtypes")
	public static String getMethodParam(MethodInvocation invocation) {
		Class[] paramClassArray = invocation.getMethod().getParameterTypes();
		if (paramClassArray != null && paramClassArray.length > 0) {
			StringBuilder paramBuilder = new StringBuilder(20);
			paramBuilder.append("[");
			for (int i = 0; i < paramClassArray.length; i++) {
				paramBuilder.append(paramClassArray[i].getSimpleName());
				if (i != paramClassArray.length - 1) {
					paramBuilder.append(",");
				}
			}
			paramBuilder.append("]");
			return paramBuilder.toString();
		}else{
			return MethodRouterConfig.DEFAULT_PARAM;
		}
	}

	public static String getMethodRouteIndexKey(MethodInvocation invocation) {
		String className = invocation.getThis().getClass().getName();
		String methodName = invocation.getMethod().getName();
		String param = getMethodParam(invocation);

		return getMethodRouteIndexKey(className, methodName, param);
	}

	public static String getMethodRouteIndexKey(String className,
			String methodName, String param) {
		return className + "_" + methodName + "_" + param;
	}
	
	public static String getFuzzyMethodRouteIndexKey(String className,
			String methodName) {
		return className + "_" + methodName.replaceAll("\\*", "");
	}

}
